package it.csi.finregio.finreges.presentation;

import it.crp.crparch.web.struts.plugin.CRPStrutsPlugin;
import it.crp.logger.PrdLoggerFactory;
import it.csi.finregio.finreges.client.initforms.FormSelector;
import it.csi.finregio.finreges.presentation.action.FinregioActionHelper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.RequestProcessor;

public class FinregioRequestProcessor extends RequestProcessor {
	
	private transient Logger log = PrdLoggerFactory.getInstance().getLogger(
			"finreges");

	protected ActionForm processActionForm(HttpServletRequest request,
			HttpServletResponse response, ActionMapping mapping){
		log.info("[FinregioRequestProcessor::processActionForm] BEGIN");
		log.info("[FinregioRequestProcessor::processActionForm()] "+request.getRequestURI());
		FinregioActionHelper.getSession(request).removeAttribute("SAVED");
		CRPStrutsPlugin plugin = (CRPStrutsPlugin) getServletContext().getAttribute(CRPStrutsPlugin.PLUGIN_NAME);
		FormSelector selector = (FormSelector) plugin.getInitializer(mapping.getPath());
		ActionForm form  = null;
		if(selector!=null){
			
			String paramValue = request.getParameter(selector.getParameterName());
			String formName = (String) selector.getFormsName().get(paramValue);
			mapping.setName(formName);//??verificare che non crei qualche pastccio nella gestione delle form
//				if("request".equals(mapping.getScope())){
//					form = request.getAttribute(formName); 
//					request.setAttribute(formName, form);
//				}
//				else{
//		            HttpSession session = request.getSession();
//		            session.setAttribute(formName, form);
//
//				}
		}
		form=super.processActionForm(request, response, mapping);
		
		System.out.println(form);
		System.out.println("*********************************************************");
		return form;
	}

}
